<template>
  <div class="app-container">
    <div class="filter-container">
      <SearchOptionUser type="2" @setSearchUserId="searchUserId" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生" align="center">
        <template slot-scope="{row}">
          <span>{{ row.userInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="信用学分" align="center" class-name="pointer-style">
        <template slot-scope="{row}">
          <span @click="handleUpdate(row,'信用分')">{{ row.credit_score }}</span>
        </template>
      </el-table-column>

      <el-table-column label="应修学分" align="center" class-name="pointer-style">
        <template slot-scope="{row}">
          <span @click="handleUpdate(row,'应修学分')">{{ row.should_score }}</span>
        </template>
      </el-table-column>

      <el-table-column label="已修学分" align="center" class-name="pointer-style">
        <template slot-scope="{row}">
          <span @click="jumpUserInfo(row, 3)">{{ row.is_should_score }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="row">
          <el-button type="primary" size="mini" @click="jumpUserInfo(row)">
            历史
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="学生信用分和应修学分编辑" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="学生姓名">
          <el-input :value="userName" disabled />
        </el-form-item>
        <div v-if="updateName === '信用分'">
          <el-form-item :label="`调整前${updateName}`">
            <el-input-number v-model="rowTemp.credit_score" disabled />
          </el-form-item>
          <el-form-item :label="`调整后${updateName}`" prop="credit_score">
            <el-input-number v-model="temp.credit_score" />
          </el-form-item>
          <el-form-item label="请输入原因" prop="content">
            <el-input v-model="temp.content" type="textarea" :rows="5" />
          </el-form-item>
        </div>
        <div v-else-if="updateName === '应修学分'">
          <el-form-item :label="`调整前${updateName}`">
            <el-input-number v-model="rowTemp.should_score" disabled />
          </el-form-item>
          <el-form-item :label="`调整后${updateName}`" prop="should_score">
            <el-input-number v-model="temp.should_score" />
          </el-form-item>
          <el-form-item label="请输入原因" prop="content">
            <el-input v-model="temp.content" type="textarea" :rows="5" />
          </el-form-item>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateName === '信用分'?updateData(updateName):updateData(updateName)">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  scoreStudentCreditscoreUpdate,
  scoreStudentPage,
  scoreStudentShouldscoreUpdate
} from '@/api/xuanxiu'
import waves from '@/directive/waves' // waves directive
import { notifySuccess, listQueryFilter } from '@/utils/tool'
import SearchOptionUser from '@/components/SearchOption/user'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ScoreStudent',
  components: { Pagination, SearchOptionUser },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        user_id: null
      },
      temp: {},
      rowTemp: {},
      dialogFormVisible: false,
      rules: {
        credit_score: [{ required: true, message: '调整后信用分必填', trigger: 'change' }],
        should_score: [{ required: true, message: '调整后应修学分必填', trigger: 'change' }],
        content: [{ required: true, message: '原因必填', trigger: 'change' }]
      },
      updateName: '',
      userName: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      scoreStudentPage(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page
        this.listLoading = false
      })
    },
    // 初始化列表
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 获取修改内容
    handleUpdate(row, name) {
      this.rowTemp = Object.assign({}, row)
      this.userName = row.userInfo.name
      this.dialogFormVisible = true
      this.updateName = name
      this.temp = {
        id: row.id
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 学生信用分和应修学分管理编辑提交
    updateData(updateName) {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          if (updateName === '信用分') {
            scoreStudentCreditscoreUpdate(tempData).then(response => {
              var rs = response.data
              const index = this.list.findIndex(v => v.id === rs.id)
              this.list.splice(index, 1, rs)
              this.dialogFormVisible = false
              notifySuccess(this, '修改成功')
              this.getList()
            })
          } else {
            scoreStudentShouldscoreUpdate(tempData).then(response => {
              var rs = response.data
              const index = this.list.findIndex(v => v.id === rs.id)
              this.list.splice(index, 1, rs)
              this.dialogFormVisible = false
              notifySuccess(this, '修改成功')
              this.getList()
            })
          }
        }
      })
    },
    // 获取输入的学生名称id
    searchUserId: function(data) { // 触发子组件城市选择-选择城市的事件
      this.listQuery.user_id = data.userId // 改变了父组件的值
      this.temp.user_id = data.userId // 改变了父组件的值
    },
    // 跳转至学生记录
    jumpUserInfo(row, type) {
      let user_name_id = ''
      let type_name = null
      if (type) { type_name = type; user_name_id = row.user_id } else { user_name_id = row.row.user_id }
      this.$router.push({
        path: '/xuanxiu/scoreStudentChange',
        query: {
          user_id: user_name_id,
          type: type_name
        }
      })
    }
  }
}
</script>
<style>
.pointer-style{
  cursor: pointer;
}
</style>
